package com.we.mexico.flink.function;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.ResultTypeQueryable;

import com.alibaba.fastjson.JSON;

/**
 * @author ：Jason
 * @date ：Created in 2023/4/30 1:14 PM
 * @description：
 * @modified By：
 * @version: 1.0
 */
public class MapToEntityFunction<T, O> implements MapFunction<T, O>, ResultTypeQueryable<O> {
    private final Class<O> targetClass;
    private final TypeInformation<O> type;

    public MapToEntityFunction(Class<O> targetClass) {
        this.targetClass = targetClass;
        this.type = TypeInformation.of(this.targetClass);
    }

    @Override
    public O map(T value) throws Exception {
        return JSON.parseObject(String.valueOf(value), targetClass);
    }

    @Override
    public TypeInformation<O> getProducedType() {
        return this.type;
    }
}
